﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fighter.Masters.ModuleData;

namespace Fighter.Fights.Libraries
{
    class ExtendedTreeNode : TreeNode
    {
        private DRClub drClub = new DRClub();
        private DRAthlete drAthlete = new DRAthlete();

        public Fight Fighter1 { get; set; }
        public Fight Fighter2 { get; set; }
        public string Key { get; private set; }

        public ExtendedTreeNode()
        {}

        public ExtendedTreeNode(string key, Fight f1, Fight f2)
        {
            Fighter1 = f1;
            Fighter2 = f2;
            Key = key;
            PrepareText();
        }

        public void AddFighter(Fight f2)
        {
            Fighter2 = f2;
            PrepareText();
        }

        public void RemoveFighter2()
        {
            Fighter2 = null;
            PrepareText();
        }

        public void RemoveFighter1()
        {
            Fighter1 = Fighter2;
            Fighter2 = null;
            PrepareText();
        }

        public override object Clone()
        {
            var obj = (ExtendedTreeNode)base.Clone();
            obj.Fighter1 = this.Fighter1;
            obj.Fighter2 = this.Fighter2;
            obj.Key = this.Key;
            return obj;
        }

        private void PrepareText()
        {
            string Club1 = drClub.Find(Fighter1.ClubMail)
                            ? drClub.GetValue<string>(FG_Club.Club)
                            : Fighter1.ClubMail;

            string Athlete1 = drAthlete.Find(Fighter1.IDAthlete)
                                        ? drAthlete.GetValue<string>(FG_Athlete.FirstName)
                                        : Fighter1.IDAthlete.ToString();

            string Club2 = Fighter2 == null || !drClub.Find(Fighter2.ClubMail)
                                    ? "" :
                                    drClub.GetValue<string>(FG_Club.Club);

            string Athlete2 = Fighter2 == null || !drAthlete.Find(Fighter2.IDAthlete)
                                        ? "" :
                                        drAthlete.GetValue<string>(FG_Athlete.FirstName);

            if (Fighter2 == null)
            {
                BackColor = System.Drawing.Color.LightCyan;
                Text = Athlete1;
                ToolTipText = string.Format("{0} [{1}] {2} Kg {3} Anni", Athlete1, Club1, Fighter1.Weight, Fighter1.Age);
            }
            else
            {
                BackColor = System.Drawing.Color.White;
                Text =  string.Concat(Athlete1, " vs ", Athlete2);
                ToolTipText = string.Format("{0} [{1}] {2} Kg {3} Anni\n{4} [{5}] {6} Kg {7} Anni",
                    Athlete1, Club1, Fighter1.Weight,Fighter1.Age,
                    Athlete2, Club2, Fighter2.Weight, Fighter2.Age);
            }
        }
    }
}
